<template>
  <view>
    <view class="header-box">
      <view class="header-top">
        <view class="navbar">
          <!-- <view class="title">我的</view> -->
        </view>
      </view>
      <view class="user-info">
        <u-avatar class="avatar-info" size="80" :src="avatar" shape="circle"></u-avatar>
        <view class="user-infos">
          <view class="user-name">{{ nickName }}</view>
          <view class="user-iphone">{{ phonenumber }}</view>
        </view>
      </view>
    </view>
    <view class="list-item">
      <view class="items" @click="infosManage()">
        <view class="icons">
          <image src="@/static/images/common/profile.png" mode="scaleToFill" />
        </view>
        <view class="names">个人中心</view>
        <u-icon name="arrow-right" size="20" />
      </view>
      <view class="items" @click="editPwdManage()">
        <view class="icons">
          <image src="@/static/images/common/pwdicon.png" mode="scaleToFill" />
        </view>
        <view class="names">密码管理</view>
        <u-icon name="arrow-right" size="20" />
      </view>
    </view>
    <view class="logout">
      <button hover-class="button-hover" @click="handleLogout()">
        退出登录
      </button>
    </view>
  </view>
</template>

<script>
  import { mapGetters, mapMutations } from "vuex";
  import config from "@/config";
  import { updateUserProfile } from "@/api/mine/user";
  export default {
    data() {
      return {
        baseUrl: config.baseUrl,
        roleName: "",
        user: {},
        showSex: false,
        sexs: [
          {
            name: "男",
            value: "0",
          },
          {
            name: "女",
            value: "1",
          },
          {
            name: "未知",
            value: "2",
          },
        ],
        sexDesc: "",
        nickName: "",
        phonenumber: "",
        avatar: "",
      };
    },
    computed: {
      ...mapGetters(["consignor"])
    },
    onLoad() {
    },
    onShow() {
      this.$store.dispatch("GetInfo").then((res) => {
        let data = res.data;

        this.avatar = data.sysUser.avatar
          ? config.baseUrl + data.sysUser.avatar
          : "/static/images/mine/user-icon.png";
        this.nickName = data.sysUser.nickName;
        this.phonenumber = data.sysUser.phonenumber;
        if (data.roleName.length > 0) {
          this.roleName = data.roleName[0];
        }
      });
    },
    methods: {
      ...mapMutations(["SET_CONSIGNOR"]),
      //跳转修改姓名
      goEditUserName() {
        this.$tab.navigateTo(
          "/pages/mine/info/editUserName?userId=" +
          this.user.sysUser.userId +
          "&nickName=" +
          this.user.sysUser.nickName
        );
      },
      infosManage() {
        this.$tab.navigateTo("/pages/mine/userInfos/infos");
      },
      editPwdManage() {
        this.$tab.navigateTo("/pages/mine/updatePwd/updatePwd");
      },
      //显示修改性别
      updateSex() {
        this.showSex = true;
      },
      //修改性别
      sexSelect(e) {
        this.sexDesc = e.name;
        this.user.sysUser.sex = e.value;
        this.SET_CONSIGNOR(this.user);
        updateUserProfile({
          userId: this.consignor.sysUser.userId,
          sex: e.value,
        }).then((response) => {
          this.$modal.msgSuccess("修改成功");
        });
      },
      chooseAvatar() {
        this.$tab.navigateTo("/pages/mine/avatar/index");
      },
      handleLogout() {
        this.$modal.confirm("确定注销并退出系统吗？").then(() => {
          this.$store.dispatch("LogOut").then(() => {
            this.$tab.reLaunch("/pages/login");
          });
        });
      },
      preview(url) {
        uni.previewImage({
          urls: [url],
          longPressActions: {
            itemList: ["发送给朋友", "保存图片", "收藏"],
            success: function (data) {
              console.log(
                "选中了第" +
                (data.tapIndex + 1) +
                "个按钮,第" +
                (data.index + 1) +
                "张图片"
              );
            },
            fail: function (err) {
              console.log(err.errMsg);
            },
          },
        });
      },
    },
  };
</script>

<style lang="scss">
  page {
    background: #f2f2f2;
  }

  .icons {
    img {
      width: 44rpx !important;
      height: 44rpx !important;
    }
  }

  .logout {
    margin: 0 30rpx;

    button {
      background-color: #d22c2c;
      color: #fff;
      font-size: 32rpx;
      position: fixed;
      bottom: 30rpx;
      width: calc(100% - 60rpx);
    }
  }

  .navbar {
    font-size: 32rpx;
    color: #ffffff;
    text-align: center;
    position: absolute;
    top: 10rpx;
    padding: 0;
    min-height: 0;
    height: 0;
  }

  .header-top {
    height: 528rpx;
    background-image: url("/static/images/common/profile_red.png");
    background-repeat: no-repeat;
    background-size: 100%;
    position: relative;

    .avatar-info {
      margin: auto;
      position: absolute;
      bottom: -30rpx;
      margin-right: 20rpx;
    }
  }

  .user-info {
    height: 140rpx;
    line-height: 140rpx;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    position: absolute;
    top: 180rpx;
    margin: 0 54rpx;

    .user-infos {
      color: #fff;
      margin-left: 24rpx;
    }

    .user-name {
      font-size: 36rpx;
      line-height: 50rpx;
      font-weight: bold;
    }

    .user-iphone,
    .user-roles {
      font-size: 30rpx;
      line-height: 40rpx;
      font-weight: 400;
    }
  }

  .marin-top-20 {
    margin-top: 40rpx;
  }

  .list-item {
    background-color: #fff;
    border-radius: 16rpx;
    min-height: 220rpx;
    margin: 0 30rpx;
    position: relative;
    top: -140rpx;

    .items {
      height: 102rpx;
      line-height: 102rpx;
      margin: 0 30rpx;
      font-size: 30rpx;
      color: #333;
      display: flex;
      align-items: center;
      justify-content: start;

      .u-icon {
        position: absolute;
        right: 20rpx;
      }

      .icons {
        display: inline-block;
        height: 22px;
        line-height: 22px;
        margin-right: 10rpx;
        width: 22px;

        uni-image {
          width: 22px;
          height: 22px;
        }
      }
    }

    .items:not(:last-child) {
      border-bottom: 2rpx solid #f1f2f5;
    }
  }
</style>